package cn.stylefeng.guns.sys.modular.communityInfo.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.communityInfo.entity.SqInfo;
import cn.stylefeng.guns.sys.modular.communityInfo.mapper.SqInfoMapper;
import cn.stylefeng.guns.sys.modular.communityInfo.model.params.SqInfoParam;
import cn.stylefeng.guns.sys.modular.communityInfo.model.result.SqInfoResult;
import cn.stylefeng.guns.sys.modular.communityInfo.service.SqInfoService;
import cn.stylefeng.guns.sys.modular.personInfo.model.params.PersonInfoParam;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 社区表 服务实现类
 * </p>
 *
 * @author WangZhen
 * @since 2021-01-20
 */
@Service
public class SqInfoServiceImpl extends ServiceImpl<SqInfoMapper, SqInfo> implements SqInfoService {

    @Override
    public void add(SqInfoParam param) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        param.setIsDel(0);
        param.setCreatePerson(user.getName());
        param.setCreateDate(new Date());
        this.baseMapper.insertCommunity(param);
    }

    @Override
    public void delete(SqInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(SqInfoParam param) {
        SqInfo oldEntity = getOldEntity(param);
        SqInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SqInfoResult findBySpec(SqInfoParam param) {
        return null;
    }

    @Override
    public List<SqInfoResult> findListBySpec(SqInfoParam param) {
        param.setIsDel(0);
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(SqInfoParam param) {
        Page pageContext = getPageContext();
        param.setIsDel(0);
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Integer selectDeleteInfo(Integer param) {
        return this.baseMapper.selectDelete(param);
    }

    @Override
    public void updateDeleteInfo(Integer param) {
        this.baseMapper.updateDelete(param);
    }

    @Override
    public void updateCommunityInfo(SqInfoParam param) {
        this.baseMapper.updateCommunity(param);
    }

    private Serializable getKey(SqInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SqInfo getOldEntity(SqInfoParam param) {
        return this.getById(getKey(param));
    }

    private SqInfo getEntity(SqInfoParam param) {
        SqInfo entity = new SqInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
